﻿using System;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace CEFdemo
{
    public struct FilePath
    {
        public string name;

        public string splitPath(string s)
        {
            string[] temp = s.Split('\\');
            return temp[temp.Length - 1];
        }

        public string GetFrameTreeResponse(string filepath)
        {
            string ret = "";
            ret += $"<ul class=\"nested active\">";
            string[] files = Directory.GetFiles(filepath, "*.md");
            foreach (string file in files)
            {
                ret += filebutton(file);
            }

            string[] dirs = Directory.GetDirectories(filepath);
            foreach (string dir in dirs)
            {
                ret += "<li><ul style=\"padding: 0\"><li>";
                ret += folderbutton(dir);
                ret += GetFrameTreeResponse(dir);
                ret += "</li></ul></li>";
            }

            ret += "</ul>";

            return ret;
        }

        public string filebutton(string name)
        {
            return
                $"<li><button type=\"button\" class=\"file-item\"  name=\"{name}\" onclick=\"openFile(this.name)\">" +
                $"{splitPath(name)}" + "</button></li>";
        }

        public string folderbutton(string name)
        {
            return $"<button class=\"caret caret-down\" name=\"{name}\">{splitPath(name)}</button>";
        }
    }

    public class CSfunction
    {
        public Form1 form;
        public string projectfolder;
        private ManualResetEvent folderSelectedEvent = new ManualResetEvent(false);
        private string downloadFolder;
        private AutoResetEvent downLoadFolderSelectedEvent = new AutoResetEvent(false);

        public CSfunction(Form1 form)
        {
            this.form = form;
        }
        
        
        public string converttohtml(string markdownString)
        {
            return ConvertHTML.convertToHtml(markdownString);
        }

        public void exitapp()
        {
            Environment.Exit(0);
        }

        public void minwindow()
        {
            form.WindowState = FormWindowState.Minimized;
        }

        public void normal()
        {
            form.WindowState = FormWindowState.Normal;
        }

        public string openfolder()
        {
            Thread t = new Thread((ThreadStart)(() =>
                    {
                        FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                        DialogResult result = folderBrowserDialog.ShowDialog();
                        if (result == DialogResult.OK)
                        {
                            projectfolder = folderBrowserDialog.SelectedPath;
                            folderSelectedEvent.Set();
                        }
                        else
                        {
                            Console.WriteLine("No folder selected.");
                            folderSelectedEvent.Set();
                        }
                    }
                ));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            t.Join();
            folderSelectedEvent.WaitOne();

            if (projectfolder != null)
                return generatefiletree();
            else
                return "";
        }

        public string openfile(string filepath)
        {
            string content = File.ReadAllText(filepath, Encoding.UTF8);
            return content;
        }

        public string generatefiletree()
        {
            FilePath obj = new FilePath();
            obj.name = projectfolder;

            string div = "<ul style=\"padding: 0\"><li>";
            div += obj.folderbutton(projectfolder);
            div += obj.GetFrameTreeResponse(projectfolder);
            div += "</li></ul>";
            return div;
        }

        public string newfile(string path, string filename)
        {
            if (path != "")
            {
                string newfilepath = path + '\\' + filename + ".md";
                File.Create(newfilepath).Close();
            }

            string div = generatefiletree();
            return div;
        }

        public string newfolder(string path, string foldername)
        {
            if (path != "")
            {
                string newfolderpath = Path.Combine(path, foldername);
                Directory.CreateDirectory(newfolderpath);
            }

            string div = generatefiletree();
            return div;
        }

        public void savefile(string filepath, string content)
        {
            if (filepath != "")
            {
                File.WriteAllText(filepath, content, Encoding.UTF8);
            }
        }

        public string deleteFolder(string path)
        {
            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }

            return generatefiletree();
        }

        public string deletefile(string path)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            return generatefiletree();
        }

        public string retprojectfolder()
        {
            return projectfolder;
        }
        
        public string selectdownloadfolder()
        {
            Thread t = new Thread((ThreadStart)(() =>
                    {
                        FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                        DialogResult result = folderBrowserDialog.ShowDialog();
                        if (result == DialogResult.OK)
                        {
                            downloadFolder = folderBrowserDialog.SelectedPath;
                            downLoadFolderSelectedEvent.Set();
                        }
                        else
                        {
                            Console.WriteLine("No folder selected.");
                            downLoadFolderSelectedEvent.Set();
                        }
                    }
                ));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            t.Join();
            downLoadFolderSelectedEvent.WaitOne();

            return downloadFolder;
        }
    }
}